package com.mall.system.controller;

import com.mall.common.result.Result;
import com.mall.system.service.EmailLogService;
import com.mall.system.service.SmsLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息控制器
 */
@Tag(name = "消息接口")
@Slf4j
@RestController
@RequestMapping("/system/message")
public class MessageController {
    
    @Autowired
    private SmsLogService smsLogService;
    
    @Autowired
    private EmailLogService emailLogService;
    
    @Operation(summary = "记录短信发送日志")
    @PostMapping("/sms/log")
    public Result<Long> saveSmsLog(@RequestParam("mobile") String mobile, 
                                  @RequestParam("content") String content, 
                                  @RequestParam("status") Integer status, 
                                  @RequestParam("errorMessage") String errorMessage) {
        try {
            Long logId = smsLogService.saveSmsLog(mobile, content, status, errorMessage);
            if (logId != null) {
                return Result.success(logId);
            } else {
                return Result.failed("记录短信发送日志失败");
            }
        } catch (Exception e) {
            log.error("记录短信发送日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新短信发送状态")
    @PostMapping("/sms/status")
    public Result<Boolean> updateSmsLogStatus(@RequestParam("logId") Long logId,
                                              @RequestParam("status") Integer status,
                                              @RequestParam("errorMessage") String errorMessage) {
        try {
            boolean success = smsLogService.updateSmsLogStatus(logId, status, errorMessage);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新短信发送状态失败");
            }
        } catch (Exception e) {
            log.error("更新短信发送状态异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "记录邮件发送日志")
    @PostMapping("/email/log")
    public Result<Long> saveEmailLog(@RequestParam("toEmail") String toEmail, 
                                    @RequestParam("subject") String subject, 
                                    @RequestParam("content") String content, 
                                    @RequestParam("status") Integer status, 
                                    @RequestParam("errorMessage") String errorMessage) {
        try {
            Long logId = emailLogService.saveEmailLog(toEmail, subject, content, status, errorMessage);
            if (logId != null) {
                return Result.success(logId);
            } else {
                return Result.failed("记录邮件发送日志失败");
            }
        } catch (Exception e) {
            log.error("记录邮件发送日志异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新邮件发送状态")
    @PostMapping("/email/status")
    public Result<Boolean> updateEmailLogStatus(@RequestParam("logId") Long logId,
                                                @RequestParam("status") Integer status,
                                                @RequestParam("errorMessage") String errorMessage) {
        try {
            boolean success = emailLogService.updateEmailLogStatus(logId, status, errorMessage);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新邮件发送状态失败");
            }
        } catch (Exception e) {
            log.error("更新邮件发送状态异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}